<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学院信息</el-breadcrumb-item>
      <el-breadcrumb-item>学院列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区-->
    <el-card>
      <!--搜索与添加-->
      <el-row :gutter="10">
        <el-col :span="8">
          <el-input placeholder="请输入学院编号" v-model="queryInfo.query" clearable @clear="getCampusList" >
            <el-button slot="append" icon="el-icon-search" @click="selectCampusList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true" >添加学院</el-button>
        </el-col>
      </el-row>
      <!--学院列表区域-->
      <el-table :data="campusList" class="tableBox" border stripe>
        <el-table-column  align="center" label="序号" type="index"></el-table-column>
        <el-table-column  align="center" label="名称" prop="campusName"></el-table-column>
        <el-table-column  align="center" label="编号" prop="campusId"></el-table-column>
        <el-table-column  align="center" label="负责人">
          <template slot-scope="scope">
            <span>{{scope.row.administrator === '' ? '暂无负责人' : scope.row.administrator}}</span>
          </template>
        </el-table-column>
        <el-table-column  align="center" label="操作" width="160px">
          <template slot-scope="scope">
            <!--修改-->
            <el-button type="primary" icon="el-icon-edit" circle @click="showEditDialog(scope.row.campusId)"></el-button>
            <!--删除-->
            <el-button type="danger" icon="el-icon-delete" circle @click="removeCampusById(scope.row.campusId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 40, 60, 80]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!--添加学院的对话框-->
      <el-dialog
        title="添加学院"
        :visible.sync="addDialogVisible"
        @close="addDialogClosed"
        width="30%">
        <!--内容主体区域-->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" >
          <el-form-item label="编号" prop="campusId">
            <el-input v-model="addForm.campusId"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="campusName">
            <el-input v-model="addForm.campusName"></el-input>
          </el-form-item>
<!--          <el-form-item label="负责人" prop="administrator">-->
<!--            <el-input v-model="addForm.administrator"></el-input>-->
<!--          </el-form-item>-->
        </el-form>
        <!--底部区域-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCampus">确 定</el-button>
        </span>
      </el-dialog>

      <!--修改用户的对话框-->
      <el-dialog
        title="修改学院"
        :visible.sync="editDialogVisible"
        width="30%"
        @close="editDialogClosed">
        <!--内容主体区域-->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px" >
          <el-form-item label="编号" prop="campusId">
            <el-input v-model="editForm.campusId" disabled></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="campusName">
            <el-input v-model="editForm.campusName"></el-input>
          </el-form-item>
<!--          <el-form-item label="负责人" prop="administrator">-->
<!--            <el-input v-model="editForm.administrator"></el-input>-->
<!--          </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCampus">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
    //  获取用户列表参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      campusList: [],
      total: 0,
      //  控制对话的显示
      addDialogVisible: false,
      //  添加用户的表单数据
      addForm: {
        campusName: '',
        campusId: '',
        administrator: ''
      },
      //  添加表单的验证规则对象
      addFormRules: {
        campusName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' }
        ],
        campusId: [
          { required: true, message: '请输入学院编号', trigger: 'blur' }
        ]
      },
      //  修改表单的验证规则对象
      editFormRules: {
        campusName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' }
        ],
        campusId: [
          { required: true, message: '请输入学院编号', trigger: 'blur' }
        ]
      },
      //  控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      //  查询到的用户信息对象
      editForm: {
        campusName: '',
        campusId: '',
        administrator: ''
      }
    }
  },
  created () {
    this.getCampusList()
  },
  methods: {
    async getCampusList () {
      const { data: res } = await this.$http.get('/campus/findAll')
      if (res.code !== 200) {
        return this.$message.error('获取学院列表失败')
      }
      this.campusList = res.data.filter(n => n).sort(
        function (a, b) {
          return a.campusId > b.campusId ? 1 : -1
        }
      )
      this.total = res.data.length
    },
    //  监听pagesize
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCampusList()
    },
    // 监听页码值改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagesize = newPage
      this.getCampusList()
    },
    //  监听添加用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    //  监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    //  点击按钮添加新用户
    addCampus () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        //  可以发起添加用户的网络请求
        const {data: res} = await this.$http.post('/campus/insert', this.addForm)

        if (res.code !== 200) {
          this.addDialogVisible = false
          return this.$message.error('添加学院失败！')
        }

        this.$message.success('添加学院成功!')
        //  隐藏添加用户的对话框
        this.addDialogVisible = false
        //  重新获取用户列表数据
        this.getCampusList()
      })
    },
    //  展示编辑用户的对话框
    async showEditDialog (id) {
      const {data: res} = await this.$http.get(`campus/findByCampusIdEquals?campusId=${id}`)

      if (res.code !== 200) {
        this.editDialogVisible = false
        return this.$message.error('获取学院信息失败！')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    //  修改用户信息并提交
    editCampus () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        //  可以发起修改用户的网络请求
        const {data: res} = await this.$http.post('campus/update', this.editForm)

        if (res.code !== 200) {
          return this.$message.error('修改学院失败！')
        }

        this.$message.success('修改学院成功!')
        //  隐藏添加用户的对话框
        this.editDialogVisible = false
        //  重新获取用户列表数据
        await this.getCampusList()
      })
    },
    //  根据id删除对应的用户信息
    async removeCampusById (id) {
      //  弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.get(`campus/delete?campusId=${id}`)
      if (res.code !== 200) {
        return this.$message.error('删除学院失败！')
      }

      this.$message.success('删除学院成功！')
      await this.getCampusList()
    }
  }
}
</script>

<style lang="less" scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }
</style>
